//设置置Vue和路由
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

//·······················································································
import Vuex from 'vuex'
Vue.use(Vuex)

//获取本地缓存
var car = JSON.parse(localStorage.getItem('car')) || []

var store = new Vuex.Store({
    state: {
        //存储共享数据的，用this.$store.state.***才能访问
        car: car
    },

    mutations: {
        //想操作store中的state值，只能通过调用mutation提供的方法，在组件直接用 this.$store.state.***操作会造成数据紊乱
        //调用方法用this.$store.commit('方法名',唯一参数,可以是对象或者数组) 和this.$emit('父组件方法名'，fn)很像
        addTocar(state, byinfo) {
            //some不可以用用else
            var flag = false;
            state.car.some(item => {
                if (item.id == byinfo.id) {
                    item.count += parseInt(byinfo.count)
                    flag = true;
                    return true;
                }
            });
            //遍历没找到，直接放进去
            if (!flag) {
                state.car.push(byinfo)
            }
            //设置 本地缓存
            localStorage.setItem('car', JSON.stringify(state.car))

        },
        //更新每个修改
        updategoodsinfo(state, goodsinfo) {
            state.car.some(item => {
                    if (item.id == goodsinfo.id) {
                        item.count = goodsinfo.count
                        return true;
                    }
                })
                //再次 本地缓存
            localStorage.setItem('car', JSON.stringify(state.car))
        },

        removeCar(state, id) {
            state.car.some((item, i) => {
                if (parseInt(item.id) == parseInt(id)) {
                    state.car.splice(i, 1)
                    return true;
                }
            })
            localStorage.setItem('car', JSON.stringify(state.car))

        },
        undateswichinfo(state, info) {
            state.car.some(item => {
                if (item.id == info.id) {
                    item.selected = info.selected;
                    return
                }
            })
            localStorage.setItem('car', JSON.stringify(state.car))
        }

    },




    getters: {
        //只提供数据，不做修改，想要修改去找mutations，this.$store.getters.***  相当于过滤器 filters
        getallCount(state) {
            var c = 0;
            state.car.forEach(item => {
                c += parseInt(item.count)
            })
            return c
        },

        getidcount(state) {
            var objidcount = {}
            state.car.forEach(item => {
                objidcount[item.id] = item.count;
            })
            return objidcount;
        },

        getselect(state) {
            var objselect = {}
            state.car.forEach(item => {
                objselect[item.id] = item.selected
            })
            return objselect
        },

        //拿价格，这可以不看
        getprice(state) {
            var price = {}
            state.car.forEach(item => {
                price[item.id] = item.price
            })
            return price
        },


        getaccounts(state) {
            var accounts = {
                count: 0,
                account: 0
            }
            state.car.forEach(item => {
                if (item.selected) {
                    accounts.count += item.count
                    accounts.account += item.count * item.price
                }
            })
            return accounts
        }











    }


})

//·······················································································
//设置 类似AJAX
// Vue.http.options.root = '/root';  //全局路径
// Vue.http.options.emulateHTTP = true;  //AJAX的post请求的参数全局配置
import VueResource from 'vue-resource'
Vue.use(VueResource)


//引进离家的 './app.vue' './router.js'
import app from './app.vue'
import router from './router.js'

//导入时间插件
import moment from 'moment'
Vue.filter('dataformat', function(dataStr, pattern = "YYYY-MM-DD HH:mm:ss") {
        return moment(dataStr).format(pattern)
    })
    //·······················································································
    //设置 iView
    // import iView from 'iview';
    // import 'iview/dist/styles/iview.css';
    // Vue.use(iView);

//设置 mui
import './lib/dist/css/mui.css'
import './lib/dist/css/icons-extra.css'

//设置 mint-ui
// import { Swipe, SwipeItem, Header, Button, Lazyload } from 'mint-ui';
// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem);
// Vue.component(Header.name, Header)
// Vue.component(Button.name, Button)
// Vue.use(Lazyload);
import MintUI from 'mint-ui'
import 'mint-ui/lib/style.css'
Vue.use(MintUI)



// 安装图片预览插件
import VuePreview from 'vue-preview'
Vue.use(VuePreview)


//·······················································································

var vm = new Vue({
    el: '#app',
    render: c => c(app),
    router,
    store
})